mod minigrep;
mod utils;

use crate::minigrep::file;
use crate::utils::config::ConfigMinigrep;
use std::{env, process};

fn main() {
    let args: Vec<String> = env::args().collect();

    let res = ConfigMinigrep::build(&args);
    if let Err(a) = res {
        println!("[let if 出现错误] {}", a)
    } else if let Ok(config) = res {
        println!(
            "[if let] Searching for '{}' in '{}'",
            config.query, config.filename
        )
    }

    match ConfigMinigrep::build(&args) {
        Err(e) => println!("[match 出现错误] {}", e),
        Ok(config) => println!(
            "[match] Searching for '{}' in '{}'",
            config.query, config.filename
        ),
    }

    let conf = ConfigMinigrep::build(&args).unwrap_or_else(|e| {
        println!("[unwrap_or_else] 出现错误 {}", e);
        process::exit(1)
    });
    println!(
        "[unwrap_or_else] Searching for '{}' in '{}'",
        conf.query, conf.filename
    );

    println!("\n开始处理");
    // 查找文件内容
    if let Err(err) = file::grep_from_conf(conf) {
        println!("[出现错误] {}", err);
    }
}
